package com.mobjam.workingtitleforagame;

import android.util.Log;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;

public class MapControl implements OnGestureListener, OnScaleGestureListener,OnDoubleTapListener {

	protected MapView mapView;
	protected MapActivity mapActivity;
	protected Map map;
	protected boolean inScaleGesture;
	protected boolean awaitingScale;
	
	public MapControl( MapActivity mapActivity, MapView mapView )
	{
		this.mapActivity = mapActivity;
		this.mapView = mapView;
		inScaleGesture = false;
		awaitingScale = false;
	}
	
	@Override
	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		Log.d("onDown ",arg0.toString());
		return true;
	}

	@Override
	public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		Log.d("onFling: ",arg0.toString());
		return true;
	}

	@Override
	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		Log.d("onLongPress: ",arg0.toString());
	}

	@Override
	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		if( inScaleGesture )
			return true;
		// TODO Auto-generated method stub
		Log.d("onScroll: ",arg0.toString());

		if( arg2 > 50 || arg3 > 50 )
		{
			return true;
		}
		mapView.scrollX( -1*arg2);
		mapView.scrollY(-1*arg3);
		return true;
	}

	@Override
	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		Log.d("onShowPress: ",arg0.toString());
	}

	@Override
	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onScale(ScaleGestureDetector arg0) {
		// TODO Auto-generated method stub
		//Log.d("onScale: " + String.valueOf(arg0.getScaleFactor()),arg0.toString());
		if( ! awaitingScale )
			return true;
		
		if (arg0.getScaleFactor() < 1.0) {
			awaitingScale = false;
			mapView.zoomOut(arg0.getFocusX(), arg0.getFocusY());
			Log.d("pinchClose",String.valueOf(arg0.getScaleFactor()));
		}
		else if( arg0.getScaleFactor() > 1.0 ){
			awaitingScale = false;
			mapView.zoomIn(arg0.getFocusX(), arg0.getFocusY());
			Log.d("pinchOpen",String.valueOf(arg0.getScaleFactor()));
		}
		return true;
	}

	@Override
	public boolean onScaleBegin(ScaleGestureDetector arg0) {
		// TODO Auto-generated method stub
		Log.d("onScaleBegin: ",arg0.toString());
		inScaleGesture = true;
		awaitingScale = true;
		return true;
	}

	@Override
	public void onScaleEnd(ScaleGestureDetector arg0) {
		// TODO Auto-generated method stub
		Log.d("onScaleEnd: ",arg0.toString());
		inScaleGesture = false;
	}

	@Override
	public boolean onDoubleTap(MotionEvent arg0) {
		// TODO Auto-generated method stub
		Log.d("onDoubleTap: ",arg0.toString());
		if( mapView.zoomedIn )
		{
			mapView.zoomOut(arg0.getX(), arg0.getY());
		}
		else
		{
			mapView.zoomIn(arg0.getX(), arg0.getY() );
		}
		return true;
	}

	@Override
	public boolean onDoubleTapEvent(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onSingleTapConfirmed(MotionEvent arg0) {
		// TODO Auto-generated method stub
		Log.d("onSingleTapConfirmed: ",arg0.toString());
		mapView.moveCharacter(arg0.getX(), arg0.getY());
		return true;
	}
}
